<SCRIPT LANGUAGE="JavaScript">
<!-- 
function OpenWindow(url, name, width, height)
{
	popupWin = window.open(url, '', 'width=' + width + ', height=' + height + ',scrollbars,resizable')
}
-->
</script>
<?
$txtNumber = null;
$cmbIdType = null;
$clientFirstName = null;
$clientLastName = null;

$sql = null;
if (isset($_POST['txtNumber'])) {
	if (isset($_POST['txtNumber'])) {
		$txtNumber = $_POST['txtNumber'];
	}
	if (isset($_POST['txtNumber'])) {
		$cmbIdType = $_POST['cmbIdType'];
	}
	if (isset($_POST['txtNumber'])) {
		$clientFirstName = $_POST['clientFirstName'];
	}
	if (isset($_POST['txtNumber'])) {
		$clientLastName = $_POST['clientLastName'];
	}
}
elseif (isset($_COOKIE)) {
	if (isset($_COOKIE['ERS_IDType'])) {
		$txtNumber = $_COOKIE['ERS_IDType'];
	}
	if (isset($_COOKIE['ERS_IDNumber'])) {
		$cmbIdType = $_COOKIE['ERS_IDNumber'];
	}
	if (isset($_COOKIE['ERS_ClientFirstName'])) {
		$clientFirstName = $_COOKIE['ERS_ClientFirstName'];
	}
	if (isset($_COOKIE['ERS_ClientLastName'])) {
		$clientLastName = $_COOKIE['ERS_ClientLastName'];
	}
}
//print_r($_COOKIE);
//print_r($_POST);
//echo "$txtNumber - $cmbIdType - $clientFirstName - $clientLastName";

if ($txtNumber) {
	
	$idnumhyphen=explode('-',$txtNumber);
	$txtNumber = "";
	for ($i=0;$i<count($idnumhyphen);$i++) 
	{
			$txtNumber=$txtNumber."". $idnumhyphen[$i];
	}
	
	$idnumspace=explode(' ',$txtNumber);
	$txtNumber = "";
	for ($i=0;$i<count($idnumspace);$i++) 
	{
		$txtNumber=$txtNumber."". $idnumspace[$i];
	}
	
	$idnumdot=explode('.',$txtNumber);
	$txtNumber = "";
	for ($i=0;$i<count($idnumdot);$i++) 
	{
		$txtNumber=$txtNumber."". $idnumdot[$i];
	}
	
	$idnumslash=explode('/',$txtNumber);
	$txtNumber = "";
	for ($i=0;$i<count($idnumslash);$i++) 
	{
		$txtNumber=$txtNumber."". $idnumslash[$i];
	}
	
	$idnumbslash=explode('\\',$txtNumber);
	$txtNumber = "";
	for ($i=0;$i<count($idnumbslash);$i++) 
	{
		$txtNumber=$txtNumber."". $idnumbslash[$i];
	}
}

$clientFirstName = trim($clientFirstName);
$clientLastName = trim($clientLastName);

$operatorFirstName = "OR";
$operatorLastName = "OR";
$operatorReference = "OR";
$noIDSelected = "Select ID Type";
$orderBy = " ORDER BY surname, givenname ";
$limitNumber = 50;
$limit = "LIMIT $limitNumber";
$where = null;
$searchCriteria = null;

if ($cmbIdType == $noIDSelected)
{
	$identification_type = "";
}
else
{
	$identification_type = 	getIdFromListBox("er_identification_type","identification_type", $cmbIdType);
}

if((strlen($identification_type)==0) && (strlen($clientFirstName)==0) && (strlen($clientLastName)==0) && (strlen($txtNumber)==0))
{

	echo "No data entered into search, please click 'Search for Client' and fill in at least one field.<br />";
	//include 'SearchBasic.php';

	include("search.php");

}
else {
	$sql = "SELECT a.client_id, a.id_reference_no, a.surname, a.givenname, b.identification_type FROM er_client as a JOIN er_identification_type as b ON a.identification_no = b.identification_no WHERE ";
	$searchCriteria = "<span style=\"font-weight: normal\">Search results for ";
	if (!empty($clientFirstName)) {
		$where = "a.givenname LIKE '$clientFirstName%' ";
		$searchCriteria .= "First Name <b>'$clientFirstName'</b> ";
	}
	if (!empty($clientLastName)) {
		if (isset($where)) {
			$where .= "AND ";
			$searchCriteria .= "AND ";
		}
		$where .= "a.surname LIKE '$clientLastName%' ";
		$searchCriteria .= "Last Name <b>'$clientLastName'</b> ";
	}
	if (!empty($identification_type)) {
		if (isset($where)) {
			$where .= "AND ";
			$searchCriteria .= "AND ";
		}
		$where .= "b.identification_no = $identification_type ";
		$searchCriteria .= "ID Type <b>'$cmbIdType'</b> ";
	}
	if (!empty($txtNumber)) {
		if (isset($where)) {
			$where .= "AND ";
			$searchCriteria .= "AND ";
		}
		$where .= "a.id_reference_no LIKE '$txtNumber%' ";
		$searchCriteria .= "ID Number <b>'$txtNumber'</b> ";
	}
	
	//echo $sql.$where.$orderBy.$limit;
	
	if (!isset($query)) {	
		$query = r_db($sql.$where.$orderBy.$limit);
	}
	else {
		$query = r_db($query);
	}
	$result = mysql_num_rows($query);
	?>
	
	<table border="0" width="100%">
		<tr>
			<td>
				<? echo $searchCriteria." (top $result results displayed)";?> - <a href="?page=search"><span style="font-weight: normal">Click here to perform a new search</font></a>
				<? if ($result == $limitNumber) {
					$countsql = "SELECT count(a.client_id) FROM er_client as a JOIN er_identification_type as b ON a.identification_no = b.identification_no WHERE ".$where;
					$totalresults = r_db($countsql);
					$row = mysql_fetch_array($totalresults);
					echo "<br />Only the top $limitNumber out of $row[0] results are displayed in surname order.  To find a specific client you may need to include more data in your search.";
				  }
				?>
			</td>
		</tr>
	</table><br />
	<table border="0" width="100%">
		<tr>
			<td width="33%">
				<span style="font-weight: bold">If the client isn't listed below:</span>
			</td>
			<td width="33%" align="center">
				<a href="javascript:void(OpenWindow('pages/form_blank.php','',710, 400));"><span style="font-weight: normal">Step 1: Print Blank Form</font></a>
			</td>
			<td align="right">
				<a href="?page=client_add"><span style="font-weight: normal">Step 2: Enter a new Client</font></a><BR>			
			</td>
		</tr>
	</table>
	<?
	
	if($result > 0)
	{
		print "<p align=\"center\">";
		print "<table cellpadding=\"3\" cellspacing=\"0\" width=\"90%\" bordercolor=\"#000000\">\n";
		print "<tr><th>Surname</th><th>Given name</th><th>ID Type</th><th>Reference Number</th></tr>\n";
		$i = 0;
		$javascript = "onmouseover=\"this.style.fontWeight='bold';\" onmouseout=\"this.style.fontWeight='normal';\"";
		for( $i; $i < $row = mysql_fetch_array($query); $i++)
		{
			print "<tr onMouseOver=\"this.bgColor='#CCCCCC'; this.style.cursor='pointer';\" onMouseOut=\"this.bgColor='".row_color($i)."';\" bgColor=\"".row_color($i)."\"; this.style.cursor='auto'; onclick=\"location.href='index.php?page=client_view&clientID=".$row['client_id']."'\">";
			print "<TD style=\"border: 1px solid black;\">" . $row['surname'] . "</TD>";
			print "<TD style=\"border: 1px solid black;\">" . $row['givenname'] . "</TD>";
			print "<TD style=\"border: 1px solid black;\">" . $row['identification_type'] . "&nbsp;</TD>";	
			print "<TD style=\"border: 1px solid black;\">" . $row['id_reference_no'] . "&nbsp;</TD>";
			//print "<TD style=\"border: 1px solid black;\" align=\"center\">";
			//print "<a href=\"?page=client_view&clientID=".$row['client_id']."\"><img src=\"images/viewClient.gif\" border=\"0\"></a>";
			//print "<input type=\"image\" name=\"imageView$i\" src=\"images/viewClient.gif\"></td>";
			print "</tr>\n";
		}
		print "</table></p>";
	}
	else
	{
		echo "<br><br><br><p><b><CENTER>No matching results found.<CENTER></b></p>";
		//include 'SearchBasic.php';
		exit();
	}
}
?>
 <!--   <script language="javascript" src="clientsearchnav.js"></script>-->
</HTML>